package com.ycxy.sh.transaction.service.impl;

import com.ycxy.sh.transaction.dao.entity.Collect;
import com.ycxy.sh.transaction.dao.vo.LayuiPageVo;
import com.ycxy.sh.transaction.ex.ServiceException;
import com.ycxy.sh.transaction.mapper.CollectMapper;
import com.ycxy.sh.transaction.service.CollectService;
import com.ycxy.sh.transaction.util.GetDate;
import com.ycxy.sh.transaction.util.KeyUtil;
import com.ycxy.sh.transaction.web.JsonResult;
import com.ycxy.sh.transaction.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  收藏 服务类
 * </p>
 *
 * @author hlt
 * @since 2019-12-21
 */
@Slf4j
@Service
@Transactional
public class CollectServiceImpl implements CollectService {
    @Autowired
    private CollectMapper collectMapper;

    /**添加收藏*/
    public void insertCollect(Collect collect, HttpSession session){
        Long collectuserid = (Long) session.getAttribute("userid");
        Integer colloperate = collect.getColloPerate();
        collect.setCollectUserId(collectuserid);

        if (StringUtils.isEmpty(collectuserid)){
            String message="请先登录";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        if(colloperate == 1){
            Collect collect1 = collectMapper.selectStatus(collect);
            if(!StringUtils.isEmpty(collect1)){
                /**更改原来的收藏信息和状态*/
                collect1.setCommodityName(collect.getCommodityName());
                collect1.setCommodityDescribe(collect.getCommodityDescribe());
                collect1.setProductLocation(collect.getProductLocation());
                collect1.setCollectTime(GetDate.strToDate());
                int row = collectMapper.update(collect);
                if (row != 1){
                    String message="收藏失败";
                    log.debug(message);
                    throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
                }
            }else{
                collect.setId(Long.valueOf(KeyUtil.genUniqueKey()));
                int row = collectMapper.insert(collect);
                if (row != 1){
                    String message="收藏失败";
                    log.debug(message);
                    throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
                }
            }
        }else {
            Collect collect1 = collectMapper.selectStatus(collect);
            /**判断是否为本人操作*/
            if (collect1.getCollectUserId().equals(collectuserid)){
                int row = collectMapper.update(collect);
                if (row != 1){
                    String message="取消失败";
                    log.debug(message);
                    throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
                }
                log.debug("取消成功");
            }
            throw new ServiceException(ServiceCode.ERR_FORBIDDEN,"操作失败！可能由于权限不足");
        }
    }

    /**
     * 取消收藏
     * @param id
     * @param session
     */
    @Override
    public void deleteCollect(Long id, HttpSession session) {
        Long collectuserid = (Long) session.getAttribute("userid");
        Collect collect = new Collect().setId(id).setCollectUserId(collectuserid);
        Collect collect1 = collectMapper.selectStatus(collect);
        /**判断是否为本人操作*/
        if (collect1.getCollectUserId().equals(collectuserid)){
            collect.setColloPerate(2);
            int i = collectMapper.update(collect);
            if (i != 1){
                String message="取消失败";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
            }
        }
        throw new ServiceException(ServiceCode.ERR_FORBIDDEN,"操作失败！可能由于权限不足");
    }

    /**
     *分页查看用户所有收藏内容
     * @param limit
     * @param page
     * @param session
     */
    @Override
    public LayuiPageVo pageUserCollect(int limit, int page, HttpSession session) {
        Long collectuserid = (Long) session.getAttribute("userid");
        List<Collect> collectList = collectMapper.pageSelectAll((page - 1) * limit, limit, collectuserid);
        Integer dataNumber = collectMapper.Count(collectuserid);
        return new LayuiPageVo("",0,dataNumber,collectList);
    }


    /**分页查看所有收藏内容*/
//    public List<Collect> selectAllCollect(Integer page,Integer count,Long collectuserid){
//        return collectMapper.pageSelectAll(page,count,collectuserid);
//    }

}
